﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MusicExplorer.Data;
using MusicExplorer.Helper;
using MusicExplorer.Internals;
using MusicExplorer.WebRepository;
using MusicExplorer.WebRepository.Interface;
using MusicExplorer.WebRepository.Service;
using MusicExplorer.Wrappers;

namespace MusicExplorer.ViewModel.Popup
{
    public sealed class ArtistPreferencesViewModel : EntityPreferencesViewModel
    {
        private WebRepositoryBase m_webRepository;

        public ArtistPreferencesViewModel(MainWindowViewModel parent, WebRepositoryBase webRepository)
            : base(parent)
        {
            m_webRepository = webRepository;

            var allArtists = ServiceWebRepository.GetRegisteredArtistsFromWebRepository(m_webRepository.WebRepository);

            foreach (var a in allArtists.Where(a => a.artist_visibility).OrderBy(a => a.artist_order).Select(a => new DetailTreeItemWrapper(a))) m_enableEntities.Add(a);
            foreach (var a in allArtists.Where(a => !a.artist_visibility).OrderBy(a => a.artist_order).Select(a => new DetailTreeItemWrapper(a))) m_disableEntities.Add(a);
        }

        public override bool CanAddEntity { get { return m_webRepository is IArtistSearchWebRepository; } }

        public override string EntityName { get { return "Artist"; } }

        public override string EntityPluralName { get { return "Artists"; } }

        public override void UpdateEntities()
        {
            base.UpdateEntities();
            foreach (var wrapper in Entities)
                (wrapper.Entity as ARTIST).is_registered = true;
        }

        protected override bool SearchEntityFromTerms()
        {
            SearchedEntitiesLog.SetLog("Search in progress...", LogTypeEnum.Loading);

            if (m_tokenSearchEntity.IsCancellationRequested)
                return false;

            var artists = ((IArtistSearchWebRepository)m_webRepository)
                .SearchArtist(m_webClientSearchEntity, m_tokenSearchEntity.Token, m_currentSearchTerms, m_currentPageSearch);

            try
            {
                artists.Wait();
            }
            catch (Exception ex)
            {
                //if (!(ex is AggregateException || ex is OperationCanceledException))
                SearchedEntitiesLog.LogError(ex);
                return false;
            }

            if (m_tokenSearchEntity.IsCancellationRequested)
                return false;

            if (artists.Result.Count > 0)
            {
                MusicExplorerHelper.ExecuteInUIThread(() =>
                {
                    foreach (var artist in artists.Result)
                    {
                        if (Entities.Select(e => e.Entity as ARTIST).Count(a => a.artist_key == artist.artist_key) == 0)
                        {
                            var a = new DetailTreeItemWrapper(artist);
                            SearchedEntitiesToAdd.Add(a);
                        }
                    }
                    LaunchTaskFromScrollViewerState();
                });
            }
            else m_nothingToSearch = true;
            
            SearchedEntitiesLog.ResetLog();
            return true;
        }
    }
}
